home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gxccman.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  17KB  |  567 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxccman.c */
  20. /* Character cache management routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzdevice.h"            /* requires gsstate.h */
  29. #include "gzcolor.h"
  30. #include "gzpath.h"
  31. #include "gxcpath.h"
  32. #include "gxdevmem.h"
  33. #include "gxchar.h"
  34. #include "gxcache.h"
  35. #include "gxfont.h"
  36. #include "gxfdir.h"
  37. #include "gxxfont.h"
  38.  
  39. extern ulong gs_next_ids(P1(uint));
  40.  
  41. /* Export the size of the cache structures. */
  42. const uint cached_char_sizeof = sizeof_cached_char;
  43. const uint cached_fm_pair_sizeof = sizeof(cached_fm_pair);
  44.  
  45. /* Forward references */
  46. void gs_purge_fm_pair(P3(gs_font_dir *, cached_fm_pair *, int));
  47. private gx_xfont *lookup_xfont_by_name(P6(gx_device *, gx_xfont_procs *, gs_font_name *, int, const cached_fm_pair *, const gs_matrix *));
  48. private cached_char *alloc_char_in_chunk(P4(gs_font_dir *, ulong, char_cache_chunk *, uint));
  49. private void shorten_cached_char(P3(gs_font_dir *, cached_char *, uint));
  50. private uint compress_scaled_bits(P4(byte *, uint, uint, int));
  51.  
  52. /* ====== Initialization ====== */
  53.  
  54. /* Initialize the character cache. */
  55. void
  56. gx_char_cache_init(register gs_font_dir *dir)
  57. {    int i;
  58.     cached_fm_pair *pair;
  59.     dir->fmcache.msize = 0;
  60.     dir->fmcache.mnext = 0;
  61.     dir->ccache.bsize = 0;
  62.     dir->ccache.csize = 0;
  63.     dir->ccache.bspace = 0;
  64.     dir->ccache.chunks = dir->ccache.initial_chunk.next =
  65.         &dir->ccache.initial_chunk;
  66.     dir->ccache.initial_chunk.size = 0;
  67.     dir->ccache.cnext = 0;
  68.     memset((char *)dir->ccache.chars, 0,
  69.            (dir->ccache.chars_mask + 1) * sizeof(cached_char *));
  70.     for ( i = dir->fmcache.mmax, pair = dir->fmcache.mdata; --i >= 0; pair++ )
  71.       fm_pair_set_free(pair);
  72. }
  73.  
  74. /* ====== Font-level routines ====== */
  75.  
  76. /* Add a font/matrix pair to the cache. */
  77. /* (This is only exported for gxccache.c.) */
  78. cached_fm_pair *
  79. gx_add_fm_pair(register gs_font_dir *dir, gs_font *font, const gs_uid *puid,
  80.   const gs_state *pgs)
  81. {    register cached_fm_pair *pair =
  82.         dir->fmcache.mdata + dir->fmcache.mnext;
  83.     cached_fm_pair *mend =
  84.         dir->fmcache.mdata + dir->fmcache.mmax;
  85.     if ( dir->fmcache.msize == dir->fmcache.mmax ) /* cache is full */
  86.     {    /* Prefer an entry with num_chars == 0, if any. */
  87.         int count;
  88.         for ( count = dir->fmcache.mmax;
  89.               --count >= 0 && pair->num_chars != 0;
  90.             )
  91.             if ( ++pair == mend ) pair = dir->fmcache.mdata;
  92.         gs_purge_fm_pair(dir, pair, 0);
  93.     }
  94.     else
  95.     {    /* Look for an empty entry.  (We know there is one.) */
  96.         while ( !fm_pair_is_free(pair) )
  97.             if ( ++pair == mend ) pair = dir->fmcache.mdata;
  98.     }
  99.     dir->fmcache.msize++;
  100.     dir->fmcache.mnext = pair + 1 - dir->fmcache.mdata;
  101.     if ( dir->fmcache.mnext == dir->fmcache.mmax )
  102.         dir->fmcache.mnext = 0;
  103.     pair->font = font;
  104.     pair->UID = *puid;
  105.     pair->mxx = pgs->char_tm.xx, pair->mxy = pgs->char_tm.xy;
  106.     pair->myx = pgs->char_tm.yx, pair->myy = pgs->char_tm.yy;
  107.     pair->num_chars = 0;
  108.     pair->xfont_tried = 0;
  109.     pair->xfont = 0;
  110.     if_debug6('k', "[k]adding pair 0x%lx: 0x%lx [%g %g %g %g]\n",
  111.           (ulong)pair, (ulong)font,
  112.           pair->mxx, pair->mxy, pair->myx, pair->myy);
  113.     return pair;
  114. }
  115.  
  116. /* Look up the xfont for a font/matrix pair. */
  117. /* (This is only exported for gxccache.c.) */
  118. void
  119. gx_lookup_xfont(const gs_state *pgs, cached_fm_pair *pair, int encoding_index)
  120. {    gx_device *dev = pgs->device->info;
  121.     gx_device *fdev = (*dev->procs->get_xfont_device)(dev);
  122.     gs_font *font = pair->font;
  123.     gx_xfont_procs *procs = (*fdev->procs->get_xfont_procs)(fdev);
  124.     gx_xfont *xf = 0;
  125.     if ( procs != 0 )
  126.     {    gs_matrix mat;
  127.         mat.xx = pair->mxx, mat.xy = pair->mxy;
  128.         mat.yx = pair->myx, mat.yy = pair->myy;
  129.         /* xfonts can outlive their invocations, */
  130.         /* but restore purges them properly. */
  131.         pair->mprocs = pgs->memory_procs;
  132.         if ( font->key_name.size != 0 )
  133.             xf = lookup_xfont_by_name(fdev, procs,
  134.                 &font->key_name, encoding_index,
  135.                 pair, &mat);
  136. #define font_name_eq(pfn1,pfn2)\
  137.   ((pfn1)->size == (pfn2)->size &&\
  138.    !memcmp((char *)(pfn1)->chars, (char *)(pfn2)->chars, (pfn1)->size))
  139.         if ( xf == 0 && font->font_name.size != 0 &&
  140.                  /* Avoid redundant lookup */
  141.              !font_name_eq(&font->font_name, &font->key_name)
  142.            )
  143.             xf = lookup_xfont_by_name(fdev, procs,
  144.                 &font->font_name, encoding_index,
  145.                 pair, &mat);
  146.         if ( xf == 0 & font->FontType != ft_composite &&
  147.              uid_is_valid(&font->data.base.UID)
  148.            )
  149.         {    /* Look for an original font with the same UID. */
  150.             gs_font_dir *pdir = font->dir;
  151.             gs_font *pfont;
  152.             for ( pfont = pdir->orig_fonts; pfont != 0;
  153.                   pfont = pfont->next
  154.                 )
  155.             {    if ( pfont->FontType != ft_composite &&
  156.                      uid_equal(&pfont->data.base.UID, &font->data.base.UID) &&
  157.                      pfont->key_name.size != 0 &&
  158.                      !font_name_eq(&font->key_name,
  159.                                &pfont->key_name)
  160.                    )
  161.                 {    xf = lookup_xfont_by_name(fdev, procs,
  162.                         &pfont->key_name,
  163.                         encoding_index, pair, &mat);
  164.                     if ( xf != 0 )
  165.                         break;
  166.                 }
  167.             }
  168.         }
  169.     }
  170.     pair->xfont = xf;
  171. }
  172.  
  173. /* ------ Internal routines ------ */
  174.  
  175. /* Purge from the caches all references to a given font/matrix pair, */
  176. /* or just character that depend on its xfont. */
  177. void
  178. gs_purge_fm_pair(gs_font_dir *dir, cached_fm_pair *pair, int xfont_only)
  179. {    int chi;
  180.     if_debug2('k', "[k]purging pair 0x%lx%s\n",
  181.           (ulong)pair, (xfont_only ? " (xfont only)" : ""));
  182.     if ( pair->xfont != 0 )
  183.     {    (*pair->xfont->common.procs->release)(pair->xfont,
  184.             pair->mprocs);
  185.         pair->xfont = 0;
  186.         pair->xfont_tried = 0;
  187.     }
  188.     for ( chi = dir->ccache.chars_mask;
  189.           pair->num_chars != 0 && chi >= 0;
  190.         )
  191.     {    cached_char **pcc = dir->ccache.chars + chi--;
  192.         while ( *pcc != 0 )
  193.         {    cached_char *cc = *pcc;
  194.             if ( cc->head.pair == pair && (!xfont_only ||
  195.                    (pair->xfont == 0 && !cc_has_bits(cc)))
  196.                )
  197.             {    cached_char *ccnext = cc->next;
  198.                 gx_free_cached_char(dir, cc);
  199.                 *pcc = ccnext;
  200.             }
  201.             else
  202.                 pcc = &cc->next;
  203.         }
  204.     }
  205.     if ( !xfont_only )
  206.     {
  207. #ifdef DEBUG
  208.         if ( pair->num_chars != 0 )
  209.         {    lprintf1("Error in gs_purge_fm_pair: num_chars =%d\n",
  210.                  pair->num_chars);
  211.         }
  212. #endif
  213.         fm_pair_set_free(pair);
  214.         dir->fmcache.msize--;
  215.     }
  216. }
  217.  
  218. /* Look up an xfont by name. */
  219. /* The caller must already have done get_xfont_device to get the proper */
  220. /* device to pass as the first argument to lookup_font. */
  221. private gx_xfont *
  222. lookup_xfont_by_name(gx_device *fdev, gx_xfont_procs *procs,
  223.   gs_font_name *pfstr, int encoding_index, const cached_fm_pair *pair,
  224.   const gs_matrix *pmat)
  225. {    gx_xfont *xf;
  226.     if_debug5('k', "[k]lookup xfont %s [%g %g %g %g]\n",
  227.           pfstr->chars, pmat->xx, pmat->xy, pmat->yx, pmat->yy);
  228.     xf = (*procs->lookup_font)(fdev,
  229.         &pfstr->chars[0], pfstr->size,
  230.         encoding_index, &pair->UID,
  231.         pmat, pair->mprocs);
  232.     if_debug1('k', "[k]... xfont=0x%lx\n", (ulong)xf);
  233.     return xf;
  234. }
  235.  
  236. /* ====== Character-level routines ====== */
  237.  
  238. /* Allocate storage for caching a rendered character. */
  239. /* If dev != NULL set up the memory device; */
  240. /* if dev == NULL, this is an xfont-only entry. */
  241. /* Return the cached_char if OK, 0 if too big. */
  242. cached_char *
  243. gx_alloc_char_bits(gs_font_dir *dir, gx_device_memory *dev,
  244.   ushort iwidth, ushort iheight)
  245. {    ulong isize, icdsize;
  246.     uint iraster;
  247.     char_cache_chunk *cck;
  248.     cached_char *cc;
  249.     gx_device_memory mdev;
  250.     gx_device_memory *pdev = dev;
  251.     if ( dev == NULL )
  252.     {    mdev = mem_mono_device;
  253.         pdev = &mdev;
  254.     }
  255.     pdev->width = iwidth;
  256.     pdev->height = iheight;
  257.     iraster = gdev_mem_raster(pdev);
  258.     if ( iraster != 0 && iheight > dir->ccache.upper / iraster )
  259.         return 0;        /* too big */
  260.     isize = gdev_mem_bitmap_size(pdev);
  261.     icdsize = isize + cached_char_sizeof;
  262.     /* Try allocating at the current position first. */
  263.     cck = dir->ccache.chunks;
  264.     cc = alloc_char_in_chunk(dir, icdsize, cck, dir->ccache.cnext);
  265.     if ( cc == 0 )
  266.     {    if ( dir->ccache.bspace < dir->ccache.bmax )
  267.         {    /* Allocate another chunk. */
  268.             char_cache_chunk *cck_prev = cck;
  269.             uint cksize = dir->ccache.bmax / 5 + 1;
  270.             uint tsize = dir->ccache.bmax - dir->ccache.bspace;
  271.             byte *cdata;
  272.             if ( cksize > tsize )
  273.                 cksize = tsize;
  274.             if ( icdsize + sizeof(cached_char_head) > cksize )
  275.                 return 0;        /* wouldn't fit */
  276.             cck = (char_cache_chunk *)gs_malloc(1, sizeof(*cck),
  277.                             "char cache chunk");
  278.             if ( cck == 0 )
  279.                 return 0;
  280.             cdata = (byte *)gs_malloc(cksize, 1,
  281.                           "char cache chunk");
  282.             if ( cdata == 0 )
  283.             {    gs_free((char *)cck, 1, sizeof(*cck),
  284.                     "char cache chunk");
  285.                 return 0;
  286.             }
  287.             cck->data = cdata;
  288.             cck->size = cksize;
  289.             cck->next = cck_prev->next;
  290.             cck_prev->next = cck;
  291.             dir->ccache.bspace += cksize;
  292.             ((cached_char_head *)cdata)->size = cksize;
  293.             ((cached_char_head *)cdata)->pair = 0;
  294.             cc = alloc_char_in_chunk(dir, icdsize, cck, 0);
  295.         }
  296.         else
  297.         {    /* Cycle through chunks. */
  298.             char_cache_chunk *cck_init = cck;
  299.             while ( (cck = cck->next) != cck_init )
  300.             {    cc = alloc_char_in_chunk(dir, icdsize, cck, 0);
  301.                 if ( cc != 0 ) break;
  302.             }
  303.             if ( cc == 0 )
  304.                 cc = alloc_char_in_chunk(dir, icdsize, cck, 0);
  305.         }
  306.         if ( cc == 0 )
  307.             return 0;
  308.         dir->ccache.chunks = cck;    /* update roving pointer */
  309.     }
  310.     if_debug4('k', "[k]adding 0x%lx:%u(%u,%u)\n",
  311.           (ulong)cc, (uint)icdsize, iwidth, iheight);
  312.     cc->xglyph = gx_no_xglyph;
  313.     cc->width = iwidth;
  314.     cc->height = iheight;
  315.     cc->raster = iraster;
  316.     cc->head.pair = 0;    /* not linked in yet */
  317.     cc->id = gx_no_bitmap_id;
  318.     if ( dev != NULL )
  319.         gx_open_cache_device(dev, cc);
  320.     return cc;
  321. }
  322.  
  323. /* Open the cache device. */
  324. void
  325. gx_open_cache_device(gx_device_memory *dev, cached_char *cc)
  326. {    byte *bits = cc_bits(cc);
  327.     dev->width = cc->width;
  328.     dev->height = cc->height;
  329.     memset((char *)bits, 0, (uint)gdev_mem_bitmap_size(dev));
  330.     dev->base = bits;
  331.     (*dev->procs->open_device)((gx_device *)dev);    /* initialize */
  332. }
  333.  
  334. /* Remove a character from the cache. */
  335. void
  336. gx_free_cached_char(gs_font_dir *dir, cached_char *cc)
  337. {    char_cache_chunk *cck = cc->chunk;
  338.     dir->ccache.chunks = cck;
  339.     dir->ccache.cnext = (byte *)cc - cck->data;
  340.     dir->ccache.csize--;
  341.     dir->ccache.bsize -= cc->head.size;
  342.     if ( cc->head.pair != 0 )
  343.        {    /* might be allocated but not added to table yet */
  344.         cc->head.pair->num_chars--;
  345.        }
  346.     if_debug2('k', "[k]freeing 0x%lx, pair=0x%lx\n",
  347.           (ulong)cc, (ulong)cc->head.pair);
  348.     cc_set_free(cc);
  349. }
  350.  
  351. /* Add a character to the cache */
  352. void
  353. gx_add_cached_char(gs_font_dir *dir, gx_device_memory *dev,
  354.   cached_char *cc, cached_fm_pair *pair, int scale)
  355. {    if_debug3('k', "[k]chaining 0x%lx: glyph=0x%lx, wmode=%d\n",
  356.           (ulong)cc, (ulong)cc->code, cc->wmode);
  357.     if ( dev != NULL )
  358.         gx_add_char_bits(dir, dev, cc, scale);
  359.     /* Add the new character at the tail of its chain. */
  360.     {    register cached_char **head =
  361.           chars_head(dir, cc->code, pair);
  362.         while ( *head != 0 ) head = &(*head)->next;
  363.         *head = cc;
  364.         cc->next = 0;
  365.         cc->head.pair = pair;
  366.         pair->num_chars++;
  367.     }
  368. }
  369.  
  370. /* Adjust the bits of a newly-rendered character, by unscaling */
  371. /* and/or compressing. */
  372. void
  373. gx_add_char_bits(gs_font_dir *dir, gx_device_memory *dev,
  374.   cached_char *cc, int scale)
  375. {    uint raster, bsize;
  376.     byte *bits = cc_bits(cc);
  377.     /* Make sure the bits are in the right order */
  378.     /* to use as a source. */
  379.     gdev_mem_ensure_byte_order(dev);
  380.     /* If the character was oversampled, compress it now. */
  381.     if ( scale != 1 )
  382.     {    cc->raster = compress_scaled_bits(bits, cc->width,
  383.                           cc->height, scale);
  384.         cc->width /= scale;
  385.         cc->height /= scale;
  386.     }
  387.     raster = cc->raster;
  388.     bsize = raster * cc->height;
  389.     /* Compress the character in place. */
  390.     /* For now, just discard leading and trailing blank rows. */
  391.     if ( raster != 0 )
  392.     {    /* Discard trailing blank rows. */
  393.         register byte *p = bits + bsize;
  394.         register uint n = bsize;
  395.         while ( n && !p[-1] ) --n, --p;
  396.         bsize = (n + raster - 1) / raster * raster;
  397.         cc->height = bsize / raster;
  398.     }
  399.     if ( bsize )
  400.     {    /* Discard leading blank rows. */
  401.         int offset;
  402.         register byte *p = bits;
  403.         while ( !*p ) ++p;
  404.         offset = (p - bits) / raster;
  405.         if ( offset )
  406.         {    uint diff = offset * raster;
  407.             bsize -= diff;
  408.             memcpy((char *)bits, (char *)bits + diff, bsize);
  409.             cc->offset.y -= int2fixed(offset);
  410.             cc->height = bsize / raster;
  411.         }
  412.     }
  413.     /* Discard the memory device overhead that follows the bits, */
  414.     /* and any space reclaimed from unscaling or blank rows. */
  415.     {    uint diff = round_down(gdev_mem_bitmap_size(dev) - bsize,
  416.                     align_cached_char_mod);
  417.         if ( diff >= sizeof(cached_char_head) )
  418.         {    shorten_cached_char(dir, cc, diff);
  419.             dir->ccache.bsize -= diff;
  420.             if_debug2('K', "[K]shortening 0x%lx by %u (mdev overhead)\n",
  421.                   (ulong)cc, diff);
  422.         }
  423.     }
  424.     /* Assign a bitmap id. */
  425.     cc->id = gs_next_ids(1);
  426. }
  427.  
  428. /* Purge from the caches all references to a given font. */
  429. void
  430. gs_purge_font_from_char_caches(gs_font_dir *dir, const gs_font *font)
  431. {    cached_fm_pair *pair = dir->fmcache.mdata;
  432.     int count = dir->fmcache.mmax;
  433.     if_debug1('k', "[k]purging font 0x%lx\n",
  434.           (ulong)font);
  435.     while ( count-- )
  436.     {    if ( pair->font == font )
  437.         {    if ( uid_is_valid(&pair->UID) )
  438.             {    /* Keep the entry. */
  439.                 pair->font = 0;
  440.             }
  441.             else
  442.                 gs_purge_fm_pair(dir, pair, 0);
  443.         }
  444.         pair++;
  445.     }
  446. }
  447.  
  448. /* ------ Internal routines ------ */
  449.  
  450. /* Allocate a character in a given chunk, which the caller will make */
  451. /* (or ensure) current. */
  452. private cached_char *
  453. alloc_char_in_chunk(gs_font_dir *dir, ulong icdsize,
  454.   char_cache_chunk *cck, uint cnext)
  455. {    uint cdsize;
  456.     cached_char_head *cch;
  457. #define hcc ((cached_char *)cch)
  458.     cached_char *cc;
  459.     uint fsize = 0;
  460.     if ( icdsize + sizeof(cached_char_head) > cck->size - cnext )
  461.     {    /* Not enough room to allocate here. */
  462.         return 0;
  463.     }
  464.     cdsize = (uint)icdsize;
  465.     /* Look for and/or free enough space. */
  466.     cch = (cached_char_head *)(cck->data + cnext);
  467.     cc = hcc;
  468.     while ( !(fsize == cdsize ||
  469.           fsize >= cdsize + sizeof(cached_char_head))
  470.           )
  471.     {    if ( !cc_head_is_free(cch) )
  472.         {    /* Free the character */
  473.             cached_char **pcc =
  474.                 chars_head(dir, hcc->code, cch->pair);
  475.             while ( *pcc != hcc )
  476.                 pcc = &(*pcc)->next;
  477.             *pcc = hcc->next; /* remove from chain */
  478.             gx_free_cached_char(dir, hcc);
  479.         }
  480.         fsize += cch->size;
  481.         if_debug2('K', "[K]merging free 0x%lx(%u)\n",
  482.               (ulong)cch, cch->size);
  483.         cc->head.size = fsize;
  484.         cch = (cached_char_head *)((byte *)cc + fsize);
  485.     }
  486. #undef hcc
  487.     cc->chunk = cck;
  488.     if ( fsize > cdsize )
  489.       { shorten_cached_char(dir, cc, fsize - cdsize);
  490.         if_debug2('K', "[K]shortening 0x%lx by %u (initial)\n",
  491.               (ulong)cc, fsize - cdsize);
  492.       }
  493.     dir->ccache.csize++;
  494.     dir->ccache.bsize += cdsize;
  495.     dir->ccache.cnext = (byte *)cc + cdsize - cck->data;
  496.     return cc;
  497. }
  498.  
  499. /* Shorten a cached character. */
  500. /* diff >= sizeof(cached_char_head). */
  501. private void
  502. shorten_cached_char(gs_font_dir *dir, cached_char *cc, uint diff)
  503. {    char_cache_chunk *cck = cc->chunk;
  504.     cached_char_head *next;
  505.     if ( (byte *)cc + cc->head.size == cck->data + dir->ccache.cnext &&
  506.          cck == dir->ccache.chunks
  507.        )
  508.         dir->ccache.cnext -= diff;
  509.     cc->head.size -= diff;
  510.     next = (cached_char_head *)((byte *)cc + cc->head.size);
  511.     if_debug2('K', "[K]shortening creates free block 0x%lx(%u)\n",
  512.           (ulong)next, diff);
  513.     cc_head_set_free(next);
  514.     next->size = diff;
  515. }
  516.  
  517. /* ------ Oversampling/scaling ------ */
  518.  
  519. /* Count the number of 1-bits in a half-byte. */
  520. static const byte half_byte_1s[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
  521.  
  522. /* Compress a 4x4- or 2x2-oversampled bitmap to 1x1 by counting 1-bits. */
  523. /* Width and height reflect the oversampling; both are multiples of scale. */
  524. /* Return the raster of the compressed bitmap. */
  525. private uint
  526. compress_scaled_bits(byte *data, uint width, uint height, int scale)
  527. {    uint threshold = scale * scale / 2;
  528.     uint sraster = ((width + 31) >> 5) << 2;
  529.     uint sskip = sraster * scale;
  530.     uint dwidth = width / scale;
  531.     uint draster = ((dwidth + 31) >> 5) << 2;
  532.     uint dskip = -((dwidth + 7) >> 3) & 3;
  533.     uint mask = (1 << scale) - 1;
  534.     byte *srow = data;
  535.     byte *d = data;
  536.     uint h;
  537.     for ( h = height; h; h -= scale )
  538.     {    byte *s = srow;
  539.         byte out_bit = 0x80;
  540.         byte out = 0;
  541.         int in_shift = 8 - scale;
  542.         uint w;
  543.         for ( w = width; w; w -= scale )
  544.         {    uint count = 0;
  545.             uint index;
  546.             for ( index = 0; index != sskip; index += sraster )
  547.                 count += half_byte_1s[(s[index] >> in_shift) & mask];
  548.             if ( count >= threshold )
  549.                 out += out_bit;
  550.             if ( (in_shift -= scale) < 0 )
  551.                 s++, in_shift += 8;
  552.             if ( !(out_bit >>= 1) )
  553.                 *d++ = out, out_bit = 0x80, out = 0;
  554.         }
  555.         if ( out_bit != 0x80 )
  556.             *d++ = out;
  557.         switch ( dskip )
  558.         {
  559.         case 3: *d++ = 0;
  560.         case 2: *d++ = 0;
  561.         case 1: *d++ = 0;
  562.         }
  563.         srow += sskip;
  564.     }
  565.     return draster;
  566. }
  567.